package com.agile.leetcode.concurrent.thread;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/6/23 21:02
 */
public class ShareTest {
    static volatile int a=0;
    private static  int count=600;//100,200,300,400,500,600,700,100000
    public static void main(String[] args) {

        new Thread(() -> {
            System.out.println("1执行");
            ArrayList<Object> list = new ArrayList<Object>();
            long l = System.nanoTime();
            for(int x=1;x<=count;x++){
                list.add(x);
            }

            while (true){
                if(list.contains(a)){
                    System.out.println(System.currentTimeMillis()+":::消费者线程打印a:"+a);
                    break;
                }
            }
        }).start();

        Thread t2= new Thread(() -> {
            System.out.println("2执行");
            // 2秒
            time(2000000000);
            for(int j=1;j<=count;j++){
//                try {
//                    TimeUnit.MILLISECONDS.sleep(10);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                a=j;
            }
 //           System.out.println(System.currentTimeMillis()+":::t2线程改变a值，a="+a);
        });
        t2.start();
        System.out.println("1主线程获取a值，a="+a);
//        while (a<=601){
//            System.out.println("主线程获取a值，a="+a);
//
//        }


    }
    //自定义睡眠方法
    private static void time(long sleeptime){
        long start = System.nanoTime();
        long end;
        do{
            end=System.nanoTime();
        }while (start+sleeptime>=end);

    }
}
